﻿using Autofac;
using CommunityToolkit.Mvvm.Input;
using DBContext;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Models.StationConfig;
using ZoneLine.Stations;

namespace ZoneLine.ViewModels
{
    public class XWStationConfigViewModel : ViewModelBase
    {
        private IModelDAL _dal;

        public XWStationConfigViewModel(IModelDAL dal)
        {
            _dal = dal;

            Task.Run(() =>
            {
                Xw010SConfig = _dal.GetModel<XW010SStationConfig>(p => true);
                if (Xw010SConfig == null)
                {
                    Xw010SConfig = new XW010SStationConfig();
                    _dal.AddModel(Xw010SConfig);
                }
            });

            Task.Run(() =>
            {
                Xw010LConfig = _dal.GetModel<XW010LStationConfig>(p => true);
                if (Xw010LConfig == null)
                {
                    Xw010LConfig = new XW010LStationConfig();
                    _dal.AddModel(Xw010LConfig);
                }
            });

            Task.Run(() =>
            {
                Xw010RConfig = _dal.GetModel<XW010RStationConfig>(p => true);
                if (Xw010RConfig == null)
                {
                    Xw010RConfig = new XW010RStationConfig();
                    _dal.AddModel(Xw010RConfig);
                }
            });
        }

        private XW010SStationConfig _xw010SConfig;

        public XW010SStationConfig Xw010SConfig
        {
            get { return _xw010SConfig; }
            set => SetProperty(ref _xw010SConfig, value);
        }

        private XW010LStationConfig _xw010LConfig;

        public XW010LStationConfig Xw010LConfig
        {
            get { return _xw010LConfig; }
            set => SetProperty(ref _xw010LConfig, value);
        }

        private XW010RStationConfig _xw010RConfig;

        public XW010RStationConfig Xw010RConfig
        {
            get { return _xw010RConfig; }
            set => SetProperty(ref _xw010RConfig, value);
        }


        private RelayCommand _saveCmd;

        public RelayCommand SaveCmd
        {
            get { return _saveCmd ?? (_saveCmd = new RelayCommand(Save)); }
        }

        private void Save()
        {
            try
            {
                _dal.UpdateModel<XW010SStationConfig>(p => true,p=>new XW010SStationConfig
                {
                    AllowWorkAddr=Xw010SConfig.AllowWorkAddr,
                    EndUploadCodeAddr=Xw010SConfig.EndUploadCodeAddr,
                    EndUploadDataAddr=Xw010SConfig.EndUploadDataAddr,
                    Ip=Xw010SConfig.Ip,
                    ReadStartAddr = Xw010SConfig.ReadStartAddr
                });
            }
            catch (Exception ex)
            {

            }

            try
            {
                _dal.UpdateModel<XW010LStationConfig>(p => true, p => new XW010LStationConfig
                {
                    AllowWorkAddr = Xw010LConfig.AllowWorkAddr,
                    EndUploadCodeAddr = Xw010LConfig.EndUploadCodeAddr,
                    EndUploadDataAddr = Xw010LConfig.EndUploadDataAddr,
                    Ip = Xw010LConfig.Ip,
                    ReadStartAddr = Xw010LConfig.ReadStartAddr
                });
            }
            catch (Exception ex)
            {

            }

            try
            {
                _dal.UpdateModel<XW010RStationConfig>(p => true, p => new XW010RStationConfig
                {
                    AllowWorkAddr = Xw010RConfig.AllowWorkAddr,
                    EndUploadCodeAddr = Xw010RConfig.EndUploadCodeAddr,
                    EndUploadDataAddr = Xw010RConfig.EndUploadDataAddr,
                    Ip = Xw010RConfig.Ip,
                    ReadStartAddr = Xw010RConfig.ReadStartAddr
                });
            }
            catch (Exception ex)
            {

            }

        }

    }
}
